<?php

class AttrFormShowModel extends Model{
	public $table = 'attr_class';
	public function attrForm($gtid=null,$data=null){

		$attrData = $this->where(array('gt_id'=>$gtid))->all();
		if(!empty($data)){
			$attrData = $data;
		}
		// p($attrData);exit;
		foreach ($attrData as $n => $attrd) {
			$function = $attrd['show_type'];
			if(empty($function))continue;
			if(!isset($attrd['av_id']))
				$attrd['av_id']=null;
			$attrValueData = $this->$function($attrd['ac_id'],$attrd['av_id']);
			$attrData[$n]['html'] = $attrValueData;
		}

		return $attrData;
	}

	public function dropdown($acid,$av_id=null){
		$attrValue = M('attr_value')->where(array('ac_id'=>$acid))->all();
		$html = "<select name='attr[{$acid}]'>";
		
		foreach ($attrValue as $n => $v) {
			$selected = '';
			if($v['av_id']==$av_id){
				$selected = " selected=''";
			}
			$html.="<option".$selected." value='".$v['av_id']."|".$v['attr_value']."'>".$v['attr_value']."</option>";
		}
		$html.= "</select>";
		return $html;
	}
	public function text($acid){

		$attrValue = M('attr_value')->where(array('ac_id'=>$acid))->all();
		$name = $acid."|".$attrValue[0]['av_id'];
		$html = "<input type='text' name='attr[{$name}]' value='".$attrValue[0]['attr_value']."'/>";
		return $html;
	}




	public function attrFormShow($gid){
		$model = M('goods_attr');
		//获得商品属性值相关信息
		$gadata = $model->where(array('gid'=>$gid))->all();

		$attrForm = array();
		$goodsData = M('goods')->where(array('gid'=>$gid))->all();
		$cid = $goodsData[0]['cid'];
		$gtid = $goodsData[0]['gtid'];
		$category = catched('category');
		$cname = $category[$cid]['cname'];
		$goodsType = catched('goodsType');

		$gtname = $goodsType[$gtid]['gtname'];
		foreach ($gadata as $key => $value) {
			$attrForm[$key]['cid'] = $cid;
			$attrForm[$key]['cname'] = $cname;
			$attrForm[$key]['gtname'] = $gtname;
			$attrForm[$key]['av_id'] = $value['avid'];
			$attrForm[$key]['attr_value'] = $value['attr_value'];
		}
		// p($attrForm);exit;
		foreach ($attrForm as $n => $d) {
			$attrValue = M('attr_value')->where(array('av_id'=>$d['av_id']))->all();
			$attrForm[$n]['ac_id'] = $attrValue[0]['ac_id'];
			
		}

		foreach ($attrForm as $n => $d) {
			$attrValue = M('attr_class')->where(array('ac_id'=>$d['ac_id']))->all();
// p($attrValue);exit;
			$attrForm[$n]['attr_name'] = $attrValue[0]['attr_name'];
			$attrForm[$n]['show_type'] = $attrValue[0]['show_type'];
			$attrForm[$n]['is_spec'] = $attrValue[0]['is_spec'];
			$attrForm[$n]['gtid'] = $attrValue[0]['gt_id'];
		}

		//将is_spec=1的数据删除掉
		$attrForm = $this->unsetSpec($attrForm);
		$data = $this->attrForm('',$attrForm);
		return $data;

	}

	//排除掉规格 is_spec = 1的数据
	public function unsetSpec($attrForm){
		foreach ($attrForm as $key => $value) {
			if($value['is_spec']!=0){
				unset($attrForm[$key]);
			}
		}
		return $attrForm;
	}


}
?>
